package ${module.package};

<#if table.hasUpdateUser() || table.hasCreateUser()>
import ${webModule}.config.WebContext;
</#if>
import ${serviceModule.package}.${table.beanName}Service;
import ${dtoModule.package}.${table.beanName}Request;
import ${dtoModule.package}.${table.beanName}Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import ${supportModule.package}.common.bo.DataResult;
import ${supportModule.package}.common.bo.PagerRequest;
import ${supportModule.package}.common.bo.PagerResponse;

/**
 * controller for ${table.beanName} <br>
 * Created by ${loginUser} on ${generateDate}.
 */
@Validated
@RestController
@RequestMapping(value="/v1/${table.stripingName}")
public class ${table.beanName}Controller {

    @Autowired
    private ${table.beanName}Service ${table.beanName?uncap_first}Service;

    /**
     * 查询
     * @param request
     * @return
     */
    @PostMapping(value = "queries")
    public DataResult<PagerResponse<${table.beanName}Response>> query(@RequestBody PagerRequest<${table.beanName}Request> request){
    return DataResult.ok(${table.beanName?uncap_first}Service.query(request));
    }

    /**
     * 可以设置基础约束
     * @param ${table.pk.fieldName}
     * @return
     */
    @GetMapping(value = "{${table.pk.fieldName}}")
    public DataResult<${table.beanName}Response> getById(@NotNull @Min(1) @PathVariable ${table.pk.fieldType.simpleName} ${table.pk.fieldName}) {
        return DataResult.ok(${table.beanName?uncap_first}Service.getById(${table.pk.fieldName}));
    }

    /**
     * 新增
     * @param ${table.beanName?uncap_first}
     * @return 新增的数据的行主键(如果自增主键)
     */
    @PostMapping(value = "")
    public DataResult<${table.pk.wrapperFieldType.simpleName}> add(@RequestBody ${table.beanName}Request ${table.beanName?uncap_first}) {
        <#if table.hasCreateUser()>
        ${table.beanName?uncap_first}.setCreateUser(WebContext.getLoginUser());
        </#if>
        return DataResult.ok(${table.beanName?uncap_first}Service.save(${table.beanName?uncap_first}));
    }

    <#if table.enablePrimitiveType == false>
    /**
     * 更新
     * @param ${table.pk.fieldName}
     * @param ${table.beanName?uncap_first}
     * @return 影响数据表的行数
     */
    @PostMapping(value = "{${table.pk.fieldName}}")
    public DataResult<Integer> update(@PathVariable("${table.pk.fieldName}") @NotNull @Min(1) ${table.pk.fieldType.simpleName} ${table.pk.fieldName}, @RequestBody ${table.beanName}Request ${table.beanName?uncap_first}) {
        ${table.beanName?uncap_first}.set${table.pk.fieldName?cap_first}(${table.pk.fieldName});
        <#if table.hasUpdateUser()>
            ${table.beanName?uncap_first}.setUpdateUser(WebContext.getLoginUser());
        </#if>
        return DataResult.ok(${table.beanName?uncap_first}Service.update(${table.beanName?uncap_first}));
    }
    </#if>

    <#if table.generateDel()>
    /**
     * 删除
     * @param ${table.pk.fieldName}
     * @return 删除是否成功
     */
    @DeleteMapping(value = "{${table.pk.fieldName}}")
    public DataResult<Boolean> delete(@NotNull @Min(1) @PathVariable ${table.pk.fieldType.simpleName} ${table.pk.fieldName}){
        <#if table.hasUpdateUser()>
        return DataResult.ok(${table.beanName?uncap_first}Service.delete(${table.pk.fieldName}, WebContext.getLoginUser()));
        <#else>
        return DataResult.ok(${table.beanName?uncap_first}Service.delete(${table.pk.fieldName}));
        </#if>
    }
    <#else>
    /**
     * 物理删除
     * @param ${table.pk.fieldName}
     * @return 删除是否成功
     */
    @DeleteMapping(value = "{${table.pk.fieldName}}")
    public DataResult<Boolean> physicalDelete(@NotNull @Min(1) @PathVariable ${table.pk.fieldType.simpleName} ${table.pk.fieldName}){
        return DataResult.ok(${table.beanName?uncap_first}Service.physicalDelete(${table.pk.fieldName}));
    }
    </#if>


}